<?php

namespace web\api\controller;

/**
 * Description of UserBase
 * 用户基类
 * @author zmh
 */
class ChatAuth extends InterfaceAuthentication {


	/**
	 * 获取用户信息
	 */
	public function userinfo() {
		$uid = $this->_post('uid');
		$accountM = new \addons\member\model\MemberAccountModel();
		$user = $accountM->getDetail($uid, 'username,real_name,nickname,head_img,phone,country_code,individuality_signature');
		if ($user) {
			$data = [
				// 'uname' => $user['username'],
				'uname' => $user['nickname']."({$user['real_name']})",
				'phone' => $user['country_code'] . $user['phone'],
				'quhao' => $user['country_code'],
				'face' => $this->base_url.$user['head_img'],
				'sphone' => $user['phone'],
				'intro' => $user['individuality_signature'],
			];
			return $this->successJSON($data);
		}
		return $this->failJSON('用户错误');
	}

	/**
	 * 获取用户金额
	 */
	public function user_balance() {
		$uid = $this->_post('uid');
		$m = new \addons\member\model\Balance();
		$datas = $m->getUserBalanceList($uid);
		$arr = [];
		foreach ($datas as $key => $data) {
			$tmp['coin_id'] = $data['type'];
			$tmp['coin_name'] = $data['name'];
			$tmp['balance'] = $data['amount'];
			$tmp['icon'] = $this->base_url.'/public/static/user/images/logo.png';
			$arr[] = $tmp;
		}
			
		return $this->successJSON($arr);
	}


	/**
	 * 获取币种信息
	 */
	public function get_coin(){
        $type_list = (new \addons\config\model\BalanceConf)->getDatas();
        $arr = [];
        foreach ($type_list as $key => $v) {
        	$tmp['cname'] = $v['name'];
        	$tmp['fee'] = 0;
        	$tmp['id'] = $v['id'];
        	$tmp['img'] = $this->base_url.'/public/static/user/images/logo.png';
        	$tmp['name'] = $v['name'];
        	$tmp['out_fee'] = 0;
        	$tmp['type'] = $v['name'];
        	$arr[] = $tmp;
        }
		return $this->successJSON($arr);

	}

	/**
	 * 用户支付  发送红包
	 */
	public function user_pay() {
		$uid = $this->_post('uid');
		if (cache('user_pay_' . $uid)) {
			$this->failJSON('访问速度太快');
		}
		cache('user_pay_' . $uid, true, 5);
		$cid = $this->_post('cid');
		$amount = $this->_post('amount');
		$orderid = $this->_post('orderid');
		$password = $this->_post('password');
		$remark = $this->_post('remark');
		// $ip = $this->_post('ip');
		$res = (new \addons\member\model\MemberAccountModel())->checkPayPwd($uid, $password);
		if (!$res) {
			return $this->failJSON('支付密码错误');
		}
		$BalanceM = new \addons\member\model\Balance();
		$userAsset = $BalanceM->getBalanceByType($uid, $cid);
		if ($amount > $userAsset['amount']) {
			return $this->failJSON('当前金额不足');
		}
		$recordM = new \addons\member\model\TradingRecord();
		$BalanceM->startTrans();
		$re_balance = $BalanceM->updateBalance($uid, $cid, $amount);
		if (!$re_balance) {
			$BalanceM->rollback();
			return $this->failJSON('扣除金额失败');
		}
		$type = 31; //记录类型 红包
		// $transferM = new \addons\block\model\BlockTransfer;
  //       $trans_id = $transferM->addTransfer($uid,0,$amount,$cid,$type,$remark);
  //       if(!$trans_id){
  //           $BalanceM->rollback();
  //           return $this->failJSON('添加区块记录失败');
  //       }
		$userRecord = $recordM->addRecord($uid, $amount, $re_balance['before_amount'], $re_balance['amount'], $cid, $type, 0, 0, $remark, $orderid);
		if (!$userRecord) {
			$BalanceM->rollback();
			return $this->failJSON('添加流水失败');
		}
		$BalanceM->commit();
		return $this->successJSON('发送红包成功');
	}

	/**
	 * 用户领取红包
	 */
	public function user_fund() {
		$uid = $this->_post('uid');
		if (cache('user_fund' . $uid)) {
			\RET::fail('访问速度太快');
		}
		cache('user_fund' . $uid, true, 1);
		$cid = $this->_post('cid');
		$sender = $this->_post('sender');
		$amount = $this->_post('amount');
		$orderid = $this->_post('orderid');
		$remark = $this->_post('remark');
		// $ip = $this->_post('ip');
		$BalanceM = new \addons\member\model\Balance();
//		$address = $BalanceM->where(['user_id'=>$uid,'type'=>1])->value('address');
//		if(!$address) return $this->failJSON('您还未开通钱包,无法领取红包');
		$recordM = new \addons\member\model\TradingRecord();
		$BalanceM->startTrans();
		$re_balance = $BalanceM->updateBalance($uid, $cid, $amount, 1);
		if (!$re_balance) {
			$BalanceM->rollback();
			return $this->failJSON('增加金额失败');
		}
		$type = 31; //记录类型 红包
		$userRecord = $recordM->addRecord($uid, $amount, $re_balance['before_amount'], $re_balance['amount'], $cid, $type, 1, 0, $remark, $orderid);
		if (!$userRecord) {
			$BalanceM->rollback();
			return $this->failJSON('添加流水失败');
		}
//		$transferM = new \addons\block\model\BlockTransfer;
//        $trans_id = $transferM->addTransfer($sender,$uid,$amount,$cid,$type,$remark);
//        if(!$trans_id){
//            $BalanceM->rollback();
//            return $this->failJSON('添加区块记录失败');
//        }
		$BalanceM->commit();
		return $this->successJSON('领取红包成功');
	}

	/**
	 * 红包退回
	 */
	public function user_refund() {
		$uid = $this->_post('uid');
		$cid = $this->_post('cid');
		$amount = $this->_post('amount');
		$orderid = $this->_post('orderid');
		$remark = $this->_post('remark');
		$ip = $this->_post('ip');
		$BalanceM = new \addons\member\model\Balance();
		$recordM = new \addons\member\model\TradingRecord();
		$BalanceM->startTrans();
		$re_balance = $BalanceM->updateBalance($uid, $cid, $amount, 1);
		if (!$re_balance) {
			$BalanceM->rollback();
			return $this->failJSON('增加金额失败');
		}
		$type = 32; //记录类型 红包
		$userRecord = $recordM->addRecord($uid, $amount, $re_balance['before_amount'], $re_balance['amount'], $cid, $type, 1, 0, $remark, $orderid);
		if (!$userRecord) {
			$BalanceM->rollback();
			return $this->failJSON('添加流水失败');
		}
		// $transferM = new \addons\block\model\BlockTransfer;
  //       $trans_id = $transferM->addTransfer(0,$uid,$amount,$cid,$type,$remark);
  //       if(!$trans_id){
  //           $BalanceM->rollback();
  //           return $this->failJSON('添加区块记录失败');
  //       }
		$BalanceM->commit();
		return $this->successJSON('退回红包成功');
	}

}
